{include file="header.html"}
<body class="gr">

<div class="navBar navBar-hidden">
	<a class="left" href="javascript:history.back()"><i class="return-back"></i></a>
	<div class="titleView">咖啡详情</div>
</div>

<div class="goods-detail main-bottom width-wrap">
	<div class="pics">
		{if count($data->pics)}
		<div class="slide">
			<ul>
				{foreach from=$data->pics name=p item=p}
				<li title="{$p->memo}"><div><a href="javascript:void(0)"><img src="{$p->pic}" /></a></div></li>
				{/foreach}
			</ul>
		</div>
		<div class="pager"></div>
		{else}
		<div class="pic" style="background-image:url({$data->default_pic});"></div>
		{/if}
	</div>
	<div class="detail">
		<div class="name">{$data->name}</div>
		<div class="price ge-bottom ge-light">
			<div><span>￥{number_format($data->price,2)}</span>　<s>原价￥{number_format($data->market_price,2)}</s></div>
		</div>
		<div class="param">
			<div class="ico1"></div>
			<span>人气 {$data->clicks}</span>
			<div class="ico2"></div>
			<span>销量 {$data->sales}</span>
		</div>
	</div>
	
	<div class="detail-param">
		<ul>
			<li>
				<font>数量：</font>
				<div class="num">
					<a href="javascript:void(0)" class="minus">-</a>
					<input type="tel" id="quantity" value="0" />
					<a href="javascript:void(0)" class="plus">+</a>
				</div>
				<div class="clear"></div>
			</li>
		</ul>
	</div>
	
	<a href="wap.php?app=comment&act=coffee_index&coffee_id={$data->id}&pagesize=10" class="comments push-ico">
		<font>查看评论</font><div>用户评价</div><span>({$data->comments})</span>
	</a>
	
	<div class="coffee-bottomView toolBar-black ge-top">
		<a class="btn" href="javascript:void(0)">选好了</a>
		<a class="cart badge" href="javascript:void(0)"><sub></sub></a>
		<div class="price">￥0.00</div>
	</div>
</div>

<input type="hidden" id="coffee_id" value="{$data->id}" />
<input type="hidden" id="coffee_shop_id" value="{if isset($coffee_shop_id)}{$coffee_shop_id}{/if}" />
{include file="footer.html"}
<script>
function getCart(){
	$.getJSON('api.php?app=coffee_cart&act=index', function(json){
		var count = 0, total_price = 0;
		if($.isArray(json.data)){
			for(var i=0; i<json.data.length; i++){
				count += json.data[i].quantity*1;
				total_price += json.data[i].price*1 * json.data[i].quantity*1;
				if(json.data[i].coffee_id*1 == $('#coffee_id').val()*1) {
					$('#quantity').val(json.data[i].quantity);
				}
			}
		}
		$('.badge sub').html(count>0?'<b>'+count+'</b>':'');
		$('.coffee-bottomView .price').html('￥'+total_price.numberFormat(2)).priceFont('bigPrice');
	});
}
function setCart(num){
	var isEdit = true, quantity = $('#quantity').val();
	if($.isNumeric(num)){
		isEdit = false;
		quantity = num;
	}
	var coffees = [{ coffee_id:$('#coffee_id').val(), quantity:quantity }];
	$.postJSON('api.php?app=coffee_cart&act=add_cart', { coffees:$.jsonString(coffees), coffee_shop_id:$('#coffee_shop_id').val(), edit:isEdit?1:0 }, function(json){
		if(json.error!=0){ $.overloadError(json.msg);return }
		getCart();
	});
}
function resize(){
	$('.goods-detail .pics, .goods-detail .pics .pic, .goods-detail .slide').autoHeight(640, 640);
	$('.goods-detail .slide').touchmove({
		title : 'title',
		drag : true,
		keydown : true,
		pager : '.pics .pager',
		offset : 'center',
		complete : function(){
			$('.pics li div').loadpic({ fill:this.height() });
		}
	});
}
$(window).resize(resize);
$(function(){
	resize();
	getCart();
	$('.price span').priceFont('bigPrice');
	$(window).scroll(function(){
		if($(window).scrollTop()>=$('.pics').height()-$('.navBar').height()){
			$('.navBar').removeClass('navBar-hidden');
		}else{
			$('.navBar').addClass('navBar-hidden');
		}
	});
	
	$('.minus').click(function(){
		var quantity = $('#quantity');
		if(quantity.val()<=0)return;
		quantity.val(quantity.val()*1-1);
		setCart(-1);
	});
	$('.plus').click(function(){
		var quantity = $('#quantity');
		quantity.val(quantity.val()*1+1);
		setCart(1);
	});
	$('#quantity').focus(function(){
		var quantity = $(this);
		if(quantity.val().length)quantity.data('quantity', quantity.val());
	}).blur(function(){
		var quantity = $(this);
		if(!quantity.val().length){
			$.overloadError('请填写数量');
			quantity.focus();
			return;
		}
		setCart();
	});
	
	$('.coffee-bottomView .btn, .coffee-bottomView .cart').click(function(){
		location.href = 'wap.php?app=coffee_cart&act=index&coffee_shop_id='+$('#coffee_shop_id').val();
	});
});
</script>